/**
 * $Id: SessionUtils.java 9761 2016-06-02 02:44:24Z huxiaowei $
 * Copyright(C) 2014-2020 easegame, All Rights Reserved.
 */
package com.treasure.service.wechat.utils;

import com.treasure.service.third.utils.UserToken;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpSession;

/**
* @Description:    java类作用描述
* @Author:         panxinwen
* @CreateDate:     2018/11/25 9:43
* @Version:        1.0
*/
public class SessionUtils {

    public static <T> void setAttribute(SessionKey key, T t) {
        getSession().setAttribute(key.name(), t);
    }

    public static <T> T getAttribute(SessionKey key) {
        return (T) getSession().getAttribute(key.name());
    }

    public static void removeAttribute(SessionKey key) {
        getSession().removeAttribute(key.name());
    }

    public static HttpSession getSession() {
        return ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest().getSession();
    }

    public static String getUserName() {
        return getUser().getUserName();
    }

    public static UserToken getUser() {
        return getAttribute(SessionKey.USER);
    }

    public static boolean isLogin() {
        return getUser() != null;
    }
}
